package zx.x.modules.yshapp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import zx.x.common.utils.R;
import zx.x.modules.app.entity.UserEntity;
import zx.x.modules.app.service.UserService;
import zx.x.modules.yshapp.dto.UserVo;
import zx.x.modules.yshapp.entity.CommunityEntity;
import zx.x.modules.yshapp.entity.DeliveryAddressEntity;
import zx.x.modules.yshapp.entity.InvitationCodeEntity;
import zx.x.modules.yshapp.entity.PointEntity;
import zx.x.modules.yshapp.service.DeliveryAddressService;
import zx.x.modules.yshapp.service.InvitationCodeService;
import zx.x.modules.yshapp.service.PointService;

import java.util.Date;
import java.util.List;


/**
 *
 * @author x
 * @email x
 */
@RestController
@RequestMapping("yshapp/user")
public class UserAppController {
    @Autowired
    private UserService userService;
    @Autowired
    private DeliveryAddressService deliveryAddressService;
    @Autowired
    private PointService pointService;
    @Autowired
    private InvitationCodeService invitationCodeService;
    /**
     * 信息
     */
    @RequestMapping("/userInfo/{id}")
    public R userInfo(@PathVariable("id") String id){
        if ( "undefined".equals(id) ) {
            return R.error(401,"请先登录");
        }else{

            UserEntity user = userService.getById(id);
            DeliveryAddressEntity deliveryAddress = deliveryAddressService
                    .getOne(new QueryWrapper<DeliveryAddressEntity>()
                            .eq("user_id", user.getUserId())
                            .eq("default_address", 0));


            UserVo userVo = new UserVo();
            userVo.setName(user.getName());
            userVo.setPoint(user.getPoint());
            userVo.setCommunityId(user.getCommunityId());
            userVo.setType(user.getType());
            userVo.setDeliveryAddress(deliveryAddress);
            userVo.setUsername(user.getUsername());
            userVo.setTx(user.getTx());

            return R.ok().put("userInfo", userVo);
        }
    }

    /**
     * 注册
     */
    @RequestMapping("/register")
    @Transactional
    public R register(UserEntity userEntity){
        if(userEntity.getInvitationCode() != null && !"".equals(userEntity.getInvitationCode())){//受邀注册
            UserEntity user = userService.getOne(new QueryWrapper<UserEntity>()
                    .eq("invitation_code", userEntity.getInvitationCode()));
            if(user != null){
                List<InvitationCodeEntity>  list = invitationCodeService.list();
                user.setPoint(user.getPoint() + list.get(0).getAddPoint());//没确定积分
                userService.updateById(user);//更新邀请者的积分
                //积分记录表增加一条
                PointEntity pointEntity = new PointEntity();
                pointEntity.setState(0);
                pointEntity.setUserId(user.getUserId().intValue());
                pointEntity.setSource(2);
                Date currentDate = new Date();
                pointEntity.setCjTime(currentDate);
                pointEntity.setPoint(list.get(0).getAddPoint());
                pointService.save(pointEntity);


            }else{
                return R.error("邀请码错误");
            }
        }
        userEntity.setInvitationCode(null);
        userService.updateById(userEntity);
        return R.ok();
    }
    /**
     * 会员过期
     */
    @Scheduled(cron = "0 0 0 ? * * ")
//    @Scheduled(cron = "0 * * ? * *")// 每天0点执行一次
    public void UserVip(){
        List<UserEntity> userList=  userService.list(
                new QueryWrapper<UserEntity>()
                        .in("type", 1, 2)
                        .eq("authentication", 0)
                        .gt("member_end_time", new Date())
        );
        for (UserEntity user : userList){
            user.setType(3);//降为普通用户
            userService.updateById(user);
        }
    }

    @RequestMapping("/update")
    public R update(UserEntity userEntity){
        userService.updateById(userEntity);
        return R.ok();
    }
}
